import Tkinter as tk


class View(object):
    def __init__(self, master, controller):
        self.controller = controller
        self.master = master
        self.list = tk.Listbox(self.master)
        self.list.pack(expand=1, fill="both")
        self.entry = tk.Entry(self.master)
        self.entry.pack(fill="x", expand=1)
        self.entry.bind("<Return>", self.enter_handler)
        self.list.bind("<Delete>", self.delete_handler)
        self.update()

    def enter_handler(self, event):
        text = self.entry.get()
        self.controller.handle_insert(text)

    def delete_handler(self, event):
        for index in self.list.curselection():
            self.controller.handle_delete(int(index))

    def update(self):
        self.list.delete(0, "end")
        for entry in self.controller.get_data():
            self.list.insert("end", entry)
